<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NewsCateModel
 *
 * @author wangnaixin
 */
namespace Admin\Model;
use Think\Model;
class NewsCateModel extends Model {
    //put your code here
    protected  $tableName = 'news_cate';
    
    protected $_auto = array ( 
        array('ctime','time',1,'function'),
        array('flag','getFlag',3,'callback')
     );
    
    public function getList($where=array()){
        $where['status'] = 1;
        return $this->where($where)->select();
    }
    
    public function getFlag(){
        $py = new \Org\Util\Pinyin();
        return $py->getFirstPY(I('cate_name'));
    }
    
    public function getCateInfoById($id){
        $cateInfo = $this->where("id = ".$id)->find();
        return $cateInfo;
    }
    
    public function delCateById($id){
        return $this->where("id = ".$id)->delete();
    }
    
    public function getCateInfoByFlag($flag){
        $where = array(
            'flag'=>array('eq',$flag)
        );
        return $this->where($where)->find();
    }
    
    public function getCateInfoByFlags($flag){
        $where = array(
            'flag'=>array('in',$flag)
        );
        return $this->where($where)->select();
    }
    
    public function selectSubCatesByFlag($flag){
        $where = array(
            'flag'=>array('eq',$flag)
        );
        $cateInfo = $this->where($where)->find();
        $where = array(
            'pid'=>array('eq',$cateInfo['id'])
        );
        $cateInfo['subCates'] = $this->where($where)->select();
        return $cateInfo;
    }

    public function getChild($id) {
        $cateIds = array();
        $where = array(
            'id' => array('exp',sprintf('=%s or pid=%s',$id,$id))
        );
        $rows = $this->where($where)->select();
        foreach ($rows as $row) {
            $cateIds[] = $row['id'];
        }
        return $cateIds;
    }
}
